//
//  TaskListTableViewCell.m
//  OAProject
//
//  Created by yhj on 2017/3/8.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "TaskListTableViewCell.h"

@interface TaskListTableViewCell ()

@property(nonatomic,strong)UIView *bgView;

// 状态图片
@property(nonatomic,strong)UIImageView *stateImage;

// 任务名字
@property(nonatomic,strong)UILabel *taskNameLabel;

// 发布任务时间
@property(nonatomic,strong)UILabel *taskTimeLabel;

@property(nonatomic,strong)UIView *lineView;

// 编辑
@property(nonatomic,strong)UIButton *editBtn;

// 删除
@property(nonatomic,strong)UIButton *endBtn;


@end

@implementation TaskListTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        // ➡️Cell类型
        self.accessoryType=UITableViewCellAccessoryNone;

        self.taskNameLabel.font=Font_Number(16);

        self.taskTimeLabel.font=Font_Number(14);

        self.editBtn.hidden=NO;

        self.endBtn.hidden=NO;

        self.backgroundColor=BGColor;
    }
    return self;
}

// bgView
-(UIView *)bgView
{
    if (!_bgView) {
        _bgView=[UIView new];
        _bgView.backgroundColor=WhiteColor;
        [self addSubview:_bgView];
        ViewBorderRadius(_bgView,kmargin/5,1,ClearColor);
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(kmargin*3/2-1);
            make.right.mas_equalTo(-kmargin*3/2+1);
            make.bottom.mas_equalTo(0);
        }];
    }
    return _bgView;
}


// lineView
-(UIView *)lineView
{
    if (!_lineView) {
        _lineView=[UIView new];
        _lineView.backgroundColor=Main_Color;
        [self.bgView addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.width.mas_equalTo(kmargin/2);
        }];
    }
    return _lineView;
}

-(UIImageView *)stateImage
{
    if (!_stateImage) {
        _stateImage=[UIImageView new];
        _stateImage.image=[UIImage imageNamed:@"state_10"];
        [self.bgView addSubview:self.stateImage];
        [_stateImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kmargin*2-1);
            make.left.mas_equalTo(self.lineView.mas_right).offset(kmargin*3/2-1);
            make.size.mas_equalTo(CGSizeMake(kmargin*5.2,20));
        }];
    }
    return _stateImage;
}


// taskNameLabel
- (UILabel *)taskNameLabel
{

    if (!_taskNameLabel) {
        _taskNameLabel=[self getLabelWithText:@"ATS集群崩溃的稳定性的测试" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.bgView addSubview:_taskNameLabel];
        [_taskNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.stateImage.mas_right).offset(kmargin);
            make.centerY.mas_equalTo(self.stateImage.mas_centerY);
        }];
    }
    return _taskNameLabel;
}

// taskTimeLabel
- (UILabel *)taskTimeLabel
{
    if (!_taskTimeLabel) {
        _taskTimeLabel=[self getLabelWithText:@"陈晨 发布于2016年2月23日 12:23" font:Font_Number(14) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
        [self.bgView addSubview:_taskTimeLabel];
        [_taskTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.stateImage.mas_bottom).offset(kmargin*5/2);
           make.left.mas_equalTo(self.stateImage.mas_left);
            make.width.mas_equalTo(APPW/2);
        }];
    }
    return _taskTimeLabel;
}

// editBtn
-(UIButton *)editBtn
{
    if (!_editBtn) {
        _editBtn=[AppUI createButtonWithSupeView:self.contentView andWithNormalImage:[UIImage imageNamed:@"EditIcon"] andWithSelectImage:[UIImage imageNamed:@"EditIcon"] andWithBtnAction:^{

      if (self.delegate&&[self.delegate respondsToSelector:@selector(editTaskWithTaskListModel:cell:)]) {
          [self.delegate editTaskWithTaskListModel:self.model cell:self];
            }
        }];
        [self.bgView addSubview:_editBtn];
        [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
             make.centerY.mas_equalTo(self.taskTimeLabel.mas_centerY);
            make.right.mas_equalTo(self.endBtn.mas_left).offset(-kmargin*2);
            make.size.mas_equalTo(CGSizeMake(kmargin*2.5,kmargin*2.5));
            make.left.mas_equalTo(self.taskTimeLabel.mas_right).offset(kmargin);
        }];
    }
    return _editBtn;
}

// endBtn
-(UIButton *)endBtn
{
    if (!_endBtn) {
        _endBtn=[AppUI createButtonWithSupeView:self.contentView andWithNormalImage:[UIImage imageNamed:@"cancelIcon"] andWithSelectImage:[UIImage imageNamed:@"cancelIcon"] andWithBtnAction:^{

        if (self.delegate&&[_delegate respondsToSelector:@selector(deleteTaskWithTaskListModel:cell:)]) {
            [self.delegate deleteTaskWithTaskListModel:self.model cell:self];
            }
        }];
        [self.bgView addSubview:_endBtn];
        [self.endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin*2);
            make.left.mas_equalTo(self.editBtn.mas_right).offset(kmargin*2);
            make.size.mas_equalTo(CGSizeMake(kmargin*2.5,kmargin*2.5));
            make.centerY.mas_equalTo(self.taskTimeLabel.mas_centerY);
        }];
    }
    return _endBtn;
}

-(void)setModel:(TaskListModel *)model
{
    _model=model;
    self.taskNameLabel.text=model.title;
    self.taskTimeLabel.text=[NSString stringWithFormat:@"%@ 发布于 %@",model.creater_name,model.create_time];

    // 编辑中
    if ([model.status isEqualToString:@"-1"])
    {
        self.stateImage.image=[UIImage imageNamed:@"state_2"];
        self.lineView.backgroundColor=HexRGB(0x13ca00);
    }
    // 等待接受
    else if ([model.status isEqualToString:@"1"])
    {
      self.stateImage.image=[UIImage imageNamed:@"no_accept"];
      self.lineView.backgroundColor=HexRGB(0x3a9bee);
    }
    // 未开始  等待接受
    else if ([model.status isEqualToString:@"3"])
    {
        self.stateImage.image=[UIImage imageNamed:@"doing"];
        self.lineView.backgroundColor=HexRGB(0x13ca00);
    }
    // 进行中
    else if ([model.status isEqualToString:@"4"])
    {
        self.stateImage.image=[UIImage imageNamed:@"doing"];
        self.lineView.backgroundColor=HexRGB(0x13ca00);
    }
    // 暂停
    else if ([model.status isEqualToString:@"5"])
    {
        self.stateImage.image=[UIImage imageNamed:@"pause"];
        self.lineView.backgroundColor=HexRGB(0xe94646);
    }
    // 拒绝
    else if ([model.status isEqualToString:@"6"])
    {
#warning 待定处理
        self.stateImage.image=[UIImage imageNamed:@"refuse_accept"];
        self.lineView.backgroundColor=HexRGB(0xef69d4);
    }
    // 取消
    else if ([model.status isEqualToString:@"7"])
    {
        self.stateImage.image=[UIImage imageNamed:@"end_task"];
        self.lineView.backgroundColor=HexRGB(0xadadada);
    }
    // 待验收
    else if ([model.status isEqualToString:@"8"])
    {
        self.stateImage.image=[UIImage imageNamed:@"no_check"];
        self.lineView.backgroundColor=HexRGB(0xffc001);
    }
    // 待评分
    else if ([model.status isEqualToString:@"9"])
    {
        self.stateImage.image=[UIImage imageNamed:@"no_rate"];
        self.lineView.backgroundColor=HexRGB(0xffc001);
    }
    // 完成
    else if ([model.status isEqualToString:@"10"])
    {
        self.stateImage.image=[UIImage imageNamed:@"done"];
        self.lineView.backgroundColor=HexRGB(0xffc001);
    }
}

@end
